<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../../forms/resources/picker-common.js"></script>
<script src="resources/calendar-picker-common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<input type=date id=date value="2011-05-01">

<script>
description('Test if step limits available dates in calendar picker.');

openPicker(document.getElementById('date'), test1);

var pw = null;
function test1() {
    pw = popupWindow;

    testDay();
    testWeek();
    testMonth();

    finishJSTest();
}

function testDay() {
    debug('Test Day constructor');

    shouldBe('new pw.Day(2000, 1, 2).year', '2000');
    shouldBe('new pw.Day(2000, 1, 2).month', '1');
    shouldBe('new pw.Day(2000, 1, 2).date', '2');

    shouldBe('new pw.Day(2000, -1, 2).year', '1999');
    shouldBe('new pw.Day(2000, -1, 2).month', '11');
    shouldBe('new pw.Day(2000, -1, 2).date', '2');

    shouldBe('new pw.Day(2000, 1, -2).year', '2000');
    shouldBe('new pw.Day(2000, 1, -2).month', '0');
    shouldBe('new pw.Day(2000, 1, -2).date', '29');

    shouldBe('new pw.Day(-2000, 1, 2).year', '-2000');
    shouldBe('new pw.Day(-2000, 1, 2).month', '1');
    shouldBe('new pw.Day(-2000, 1, 2).date', '2');

    shouldBe('new pw.Day(2000, 1, 33).year', '2000');
    shouldBe('new pw.Day(2000, 1, 33).month', '2');
    shouldBe('new pw.Day(2000, 1, 33).date', '4');

    shouldBe('new pw.Day(200000, 1, 2).year', '200000');
    shouldBe('new pw.Day(200000, 1, 2).month', '1');
    shouldBe('new pw.Day(200000, 1, 2).date', '2');

    debug('Test Day.equals');

    shouldBeTrue('new pw.Day(2000, 1, 2).equals(new pw.Day(2000, 1, 2))');
    shouldBeTrue('new pw.Day(2000, 1, 2).equals(new pw.Day(2000, 2, -27))');
    shouldBeTrue('new pw.Day(2000, 1, 2).equals(new pw.Day(2001, 1, -364))');
    
    shouldBeFalse('new pw.Day(2000, 1, 2).equals(new pw.Day(2000, 1, 3))');
    shouldBeFalse('new pw.Day(2000, 1, 2).equals(new pw.Day(2000, 2, 2))');
    shouldBeFalse('new pw.Day(2000, 1, 2).equals(new pw.Day(2001, 1, 2))');
    shouldBeFalse('new pw.Day(2000, 1, 2).equals(new pw.Day(2001, 2, 3))');

    debug('Test Day.previous');

    shouldBeEqualToString('new pw.Day(2000, 2, 1).previous().toString()', '2000-02-29');
    shouldBeEqualToString('new pw.Day(2000, 2, 1).previous(1).toString()', '2000-02-29');
    shouldBeEqualToString('new pw.Day(2000, 2, 1).previous(2).toString()', '2000-02-28');
    shouldBeEqualToString('new pw.Day(2000, 2, 1).previous(3).toString()', '2000-02-27');
    shouldBeEqualToString('new pw.Day(2000, 2, 1).previous(30).toString()', '2000-01-31');

    debug('Test Day.next');

    shouldBeEqualToString('new pw.Day(2000, 2, 1).next().toString()', '2000-03-02');
    shouldBeEqualToString('new pw.Day(2000, 2, 1).next(1).toString()', '2000-03-02');
    shouldBeEqualToString('new pw.Day(2000, 2, 1).next(2).toString()', '2000-03-03');
    shouldBeEqualToString('new pw.Day(2000, 2, 1).next(3).toString()', '2000-03-04');
    shouldBeEqualToString('new pw.Day(2000, 2, 1).next(30).toString()', '2000-03-31');

    debug('Test Day.firstDay');

    shouldBeEqualToString('new pw.Day(2000, 2, 1).firstDay().toString()', '2000-03-01');

    debug('Test Day.middleDay');

    shouldBeEqualToString('new pw.Day(2000, 2, 1).middleDay().toString()', '2000-03-01');

    debug('Test Day.lastDay');

    shouldBeEqualToString('new pw.Day(2000, 2, 1).lastDay().toString()', '2000-03-01');

    debug('Test Day.valueOf');

    shouldBe('new pw.Day(1970, 0, 1).valueOf()', '0');
    shouldBe('new pw.Day(1970, 0, 2).valueOf()', '86400000');
    shouldBe('new pw.Day(2000, 2, 1).valueOf()', '951868800000');
    shouldBe('new pw.Day(1, 2, 1).valueOf()', '-62130499200000');
    shouldBe('new pw.Day(20000, 2, 1).valueOf()', '568977004800000');
    
    debug('Test Day.toString');
    
    shouldBeEqualToString('new pw.Day(2000, 2, 1).toString()', '2000-03-01');
    shouldBeEqualToString('new pw.Day(1, 2, 1).toString()', '0001-03-01');
    shouldBeEqualToString('new pw.Day(20000, 2, 1).toString()', '20000-03-01');

    debug('Test Day.parse');
    
    shouldBeEqualToString('pw.Day.parse("2000-02-02").toString()', '2000-02-02');
    shouldBeEqualToString('pw.Day.parse("1999-02-02").toString()', '1999-02-02');
    shouldBeEqualToString('pw.Day.parse("2000-01-02").toString()', '2000-01-02');
    shouldBeEqualToString('pw.Day.parse("2000-02-01").toString()', '2000-02-01');
    shouldBeEqualToString('pw.Day.parse("0001-01-01").toString()', '0001-01-01');
    // Incorrect number of leading zeros
    shouldBeEqualToString('pw.Day.parse("02000-02-02").toString()', '2000-02-02');
    shouldBeEqualToString('pw.Day.parse("2000-002-02").toString()', '2000-02-02');
    shouldBeEqualToString('pw.Day.parse("2000-02-002").toString()', '2000-02-02');
    shouldBeEqualToString('pw.Day.parse("2000-2-02").toString()', '2000-02-02');
    shouldBeEqualToString('pw.Day.parse("2000-02-2").toString()', '2000-02-02');
    shouldBeEqualToString('pw.Day.parse("1-02-02").toString()', '0001-02-02');
    // Invalid
    shouldBeNull('pw.Day.parse("2000-02")');
    shouldBeNull('pw.Day.parse("2000-W02")');
    shouldBeNull('pw.Day.parse("x")');
    shouldBeNull('pw.Day.parse("")');
}

function testWeek() {
    debug('Test Week constructor');

    shouldBe('new pw.Week(2000, 1).year', '2000');
    shouldBe('new pw.Week(2000, 1).week', '1');

    shouldBe('new pw.Week(2000, -1).year', '1999');
    shouldBe('new pw.Week(2000, -1).week', '51');

    shouldBe('new pw.Week(-2000, 1).year', '-2000');
    shouldBe('new pw.Week(-2000, 1).week', '1');

    shouldBe('new pw.Week(2000, 55).year', '2001');
    shouldBe('new pw.Week(2000, 55).week', '3');

    shouldBe('new pw.Week(200000, 1).year', '200000');
    shouldBe('new pw.Week(200000, 1).week', '1');

    debug('Test Week.equals');

    shouldBeTrue('new pw.Week(2000, 1).equals(new pw.Week(2000, 1))');
    shouldBeTrue('new pw.Week(2000, 1).equals(new pw.Week(2001, -51))');
    shouldBeTrue('new pw.Week(2000, 1).equals(new pw.Week(1999, 53))');
    
    shouldBeFalse('new pw.Week(2000, 1).equals(new pw.Week(2000, 2))');
    shouldBeFalse('new pw.Week(2000, 1).equals(new pw.Week(2001, 1))');
    shouldBeFalse('new pw.Week(2000, 1).equals(new pw.Week(2001, 2))');

    debug('Test Week.previous');

    shouldBeEqualToString('new pw.Week(2000, 2).previous().toString()', '2000-W01');
    shouldBeEqualToString('new pw.Week(2000, 2).previous(1).toString()', '2000-W01');
    shouldBeEqualToString('new pw.Week(2000, 2).previous(2).toString()', '1999-W52');
    shouldBeEqualToString('new pw.Week(2000, 2).previous(3).toString()', '1999-W51');
    shouldBeEqualToString('new pw.Week(2000, 2).previous(100).toString()', '1998-W07');

    debug('Test Week.next');

    shouldBeEqualToString('new pw.Week(2000, 2).next().toString()', '2000-W03');
    shouldBeEqualToString('new pw.Week(2000, 2).next(1).toString()', '2000-W03');
    shouldBeEqualToString('new pw.Week(2000, 2).next(2).toString()', '2000-W04');
    shouldBeEqualToString('new pw.Week(2000, 2).next(3).toString()', '2000-W05');
    shouldBeEqualToString('new pw.Week(2000, 2).next(100).toString()', '2001-W50');

    debug('Test Week.firstDay');

    shouldBeEqualToString('new pw.Week(2000, 2).firstDay().toString()', '2000-01-10');
    shouldBeEqualToString('new pw.Week(2000, 50).firstDay().toString()', '2000-12-11');
    shouldBeEqualToString('new pw.Week(1, 1).firstDay().toString()', '0001-01-01');
    shouldBeEqualToString('new pw.Week(1, 50).firstDay().toString()', '0001-12-10');
    shouldBeEqualToString('new pw.Week(20000, 1).firstDay().toString()', '20000-01-03');
    shouldBeEqualToString('new pw.Week(20000, 50).firstDay().toString()', '20000-12-11');

    debug('Test Week.middleDay');

    shouldBeEqualToString('new pw.Week(2000, 2).middleDay().toString()', '2000-01-13');
    shouldBeEqualToString('new pw.Week(2000, 50).middleDay().toString()', '2000-12-14');
    shouldBeEqualToString('new pw.Week(1, 1).middleDay().toString()', '0001-01-04');
    shouldBeEqualToString('new pw.Week(1, 50).middleDay().toString()', '0001-12-13');
    shouldBeEqualToString('new pw.Week(20000, 1).middleDay().toString()', '20000-01-06');
    shouldBeEqualToString('new pw.Week(20000, 50).middleDay().toString()', '20000-12-14');


    debug('Test Week.lastDay');

    shouldBeEqualToString('new pw.Week(2000, 2).lastDay().toString()', '2000-01-16');
    shouldBeEqualToString('new pw.Week(2000, 50).lastDay().toString()', '2000-12-17');
    shouldBeEqualToString('new pw.Week(1, 1).lastDay().toString()', '0001-01-07');
    shouldBeEqualToString('new pw.Week(1, 50).lastDay().toString()', '0001-12-16');
    shouldBeEqualToString('new pw.Week(20000, 1).lastDay().toString()', '20000-01-09');
    shouldBeEqualToString('new pw.Week(20000, 50).lastDay().toString()', '20000-12-17');

    debug('Test Week.valueOf');

    shouldBe('new pw.Week(1970, 1).valueOf()', '-259200000');
    shouldBe('new pw.Week(1970, 2).valueOf()', '345600000');
    shouldBe('new pw.Week(2000, 1).valueOf()', '946857600000');
    shouldBe('new pw.Week(1, 1).valueOf()', '-62135596800000');
    shouldBe('new pw.Week(20000, 2).valueOf()', '568972598400000');

    debug('Test Week.toString');
    
    shouldBeEqualToString('new pw.Week(2000, 2).toString()', '2000-W02');
    shouldBeEqualToString('new pw.Week(1, 50).toString()', '0001-W50');
    shouldBeEqualToString('new pw.Week(20000, 2).toString()', '20000-W02');

    debug('Test Week.parse');
    
    shouldBeEqualToString('pw.Week.parse("2000-W02").toString()', '2000-W02');
    shouldBeEqualToString('pw.Week.parse("1999-W10").toString()', '1999-W10');
    shouldBeEqualToString('pw.Week.parse("0001-W23").toString()', '0001-W23');
    shouldBeEqualToString('pw.Week.parse("12345-W32").toString()', '12345-W32');
    // Incorrect number of leading zeros
    shouldBeEqualToString('pw.Week.parse("02000-W02").toString()', '2000-W02');
    shouldBeEqualToString('pw.Week.parse("2000-W002").toString()', '2000-W02');
    shouldBeEqualToString('pw.Week.parse("2000-W2").toString()', '2000-W02');
    shouldBeEqualToString('pw.Week.parse("1-W02").toString()', '0001-W02');
    // Invalid
    shouldBeNull('pw.Week.parse("2000-02-03")');
    shouldBeNull('pw.Week.parse("2000-02")');
    shouldBeNull('pw.Week.parse("x")');
    shouldBeNull('pw.Week.parse("")');
}

function testMonth() {
    
    
    debug('Test Month constructor');

    shouldBe('new pw.Month(2000, 1).year', '2000');
    shouldBe('new pw.Month(2000, 1).month', '1');

    shouldBe('new pw.Month(2000, -1).year', '1999');
    shouldBe('new pw.Month(2000, -1).month', '11');

    shouldBe('new pw.Month(-2000, 1).year', '-2000');
    shouldBe('new pw.Month(-2000, 1).month', '1');

    shouldBe('new pw.Month(2000, 13).year', '2001');
    shouldBe('new pw.Month(2000, 13).month', '1');

    shouldBe('new pw.Month(200000, 1).year', '200000');
    shouldBe('new pw.Month(200000, 1).month', '1');

    debug('Test Month.equals');

    shouldBeTrue('new pw.Month(2000, 1).equals(new pw.Month(2000, 1))');
    shouldBeTrue('new pw.Month(2000, 1).equals(new pw.Month(2001, -11))');
    shouldBeTrue('new pw.Month(2000, 1).equals(new pw.Month(1999, 13))');
    
    shouldBeFalse('new pw.Month(2000, 1).equals(new pw.Month(2000, 2))');
    shouldBeFalse('new pw.Month(2000, 1).equals(new pw.Month(2001, 1))');
    shouldBeFalse('new pw.Month(2000, 1).equals(new pw.Month(2001, 2))');

    debug('Test Month.previous');

    shouldBeEqualToString('new pw.Month(2000, 2).previous().toString()', '2000-02');
    shouldBeEqualToString('new pw.Month(2000, 2).previous(1).toString()', '2000-02');
    shouldBeEqualToString('new pw.Month(2000, 2).previous(2).toString()', '2000-01');
    shouldBeEqualToString('new pw.Month(2000, 2).previous(3).toString()', '1999-12');
    shouldBeEqualToString('new pw.Month(2000, 2).previous(100).toString()', '1991-11');

    debug('Test Month.next');

    shouldBeEqualToString('new pw.Month(2000, 2).next().toString()', '2000-04');
    shouldBeEqualToString('new pw.Month(2000, 2).next(1).toString()', '2000-04');
    shouldBeEqualToString('new pw.Month(2000, 2).next(2).toString()', '2000-05');
    shouldBeEqualToString('new pw.Month(2000, 2).next(3).toString()', '2000-06');
    shouldBeEqualToString('new pw.Month(2000, 2).next(100).toString()', '2008-07');

    debug('Test Month.firstDay');

    shouldBeEqualToString('new pw.Month(2000, 1).firstDay().toString()', '2000-02-01');
    shouldBeEqualToString('new pw.Month(2000, 11).firstDay().toString()', '2000-12-01');
    shouldBeEqualToString('new pw.Month(1, 0).firstDay().toString()', '0001-01-01');
    shouldBeEqualToString('new pw.Month(1, 1).firstDay().toString()', '0001-02-01');
    shouldBeEqualToString('new pw.Month(1, 11).firstDay().toString()', '0001-12-01');
    shouldBeEqualToString('new pw.Month(20000, 0).firstDay().toString()', '20000-01-01');
    shouldBeEqualToString('new pw.Month(20000, 1).firstDay().toString()', '20000-02-01');
    shouldBeEqualToString('new pw.Month(20000, 11).firstDay().toString()', '20000-12-01');

    debug('Test Month.middleDay');

    shouldBeEqualToString('new pw.Month(2000, 0).middleDay().toString()', '2000-01-15');
    shouldBeEqualToString('new pw.Month(2000, 1).middleDay().toString()', '2000-02-14');
    shouldBeEqualToString('new pw.Month(2000, 2).middleDay().toString()', '2000-03-15');
    shouldBeEqualToString('new pw.Month(2000, 11).middleDay().toString()', '2000-12-15');
    shouldBeEqualToString('new pw.Month(1, 0).middleDay().toString()', '0001-01-15');
    shouldBeEqualToString('new pw.Month(1, 1).middleDay().toString()', '0001-02-14');
    shouldBeEqualToString('new pw.Month(1, 2).middleDay().toString()', '0001-03-15');
    shouldBeEqualToString('new pw.Month(1, 11).middleDay().toString()', '0001-12-15');
    shouldBeEqualToString('new pw.Month(20000, 0).middleDay().toString()', '20000-01-15');
    shouldBeEqualToString('new pw.Month(20000, 1).middleDay().toString()', '20000-02-14');
    shouldBeEqualToString('new pw.Month(20000, 2).middleDay().toString()', '20000-03-15');
    shouldBeEqualToString('new pw.Month(20000, 11).middleDay().toString()', '20000-12-15');


    debug('Test Month.lastDay');

    shouldBeEqualToString('new pw.Month(2000, 1).lastDay().toString()', '2000-02-29');
    shouldBeEqualToString('new pw.Month(2000, 11).lastDay().toString()', '2000-12-31');
    shouldBeEqualToString('new pw.Month(1, 0).lastDay().toString()', '0001-01-31');
    shouldBeEqualToString('new pw.Month(1, 1).lastDay().toString()', '0001-02-28');
    shouldBeEqualToString('new pw.Month(1, 11).lastDay().toString()', '0001-12-31');
    shouldBeEqualToString('new pw.Month(20000, 0).lastDay().toString()', '20000-01-31');
    shouldBeEqualToString('new pw.Month(20000, 1).lastDay().toString()', '20000-02-29');
    shouldBeEqualToString('new pw.Month(20000, 11).lastDay().toString()', '20000-12-31');

    debug('Test Month.valueOf');

    shouldBe('new pw.Month(1970, 0).valueOf()', '0');
    shouldBe('new pw.Month(1970, 1).valueOf()', '1');
    shouldBe('new pw.Month(2000, 0).valueOf()', '360');
    shouldBe('new pw.Month(1, 0).valueOf()', '-23628');
    shouldBe('new pw.Month(20000, 11).valueOf()', '216371');

    debug('Test Month.toString');
    
    shouldBeEqualToString('new pw.Month(2000, 0).toString()', '2000-01');
    shouldBeEqualToString('new pw.Month(1, 11).toString()', '0001-12');
    shouldBeEqualToString('new pw.Month(20000, 5).toString()', '20000-06');

    debug('Test Month.parse');
    
    shouldBeEqualToString('pw.Month.parse("2000-01").toString()', '2000-01');
    shouldBeEqualToString('pw.Month.parse("1999-10").toString()', '1999-10');
    shouldBeEqualToString('pw.Month.parse("0001-11").toString()', '0001-11');
    shouldBeEqualToString('pw.Month.parse("12345-03").toString()', '12345-03');
    // Incorrect number of leading zeros
    shouldBeEqualToString('pw.Month.parse("02000-02").toString()', '2000-02');
    shouldBeEqualToString('pw.Month.parse("2000-002").toString()', '2000-02');
    shouldBeEqualToString('pw.Month.parse("2000-2").toString()', '2000-02');
    shouldBeEqualToString('pw.Month.parse("1-02").toString()', '0001-02');
    // Invalid
    shouldBeNull('pw.Month.parse("2000-02-03")');
    shouldBeNull('pw.Month.parse("2000-W02")');
    shouldBeNull('pw.Month.parse("x")');
    shouldBeNull('pw.Month.parse("")');
}
</script>
</body>
</html>
